#!/bin/bash
# Created by Russell Harmon (eatnumber1) <eatnumber1@gmail.com>
PS2EMUDIR=GAMES_LIBDIR/ps2emu
PS2BINDIR=GAMES_PREFIX_OPT/pcsx2
PS2DIR=GAMES_DATADIR/pcsx2

FOLDERS="memcards bios help Langs logs snap sstates patches plugins .pixmaps"
FILES="A39517AB.xml pcsx2"
CONFIGFOLDERS="inis plugins/cfg"
MASK="\.keep"
SEARCH_FOLDERS="$PS2EMUDIR $PS2BINDIR $PS2DIR"

mkdir -p ~/.pcsx2
cd ~/.pcsx2
cleanlinks
mkdir -p $FOLDERS $CONFIGFOLDERS
shopt -s nullglob

# This function is used to insert the single files to the tree.
# Usage: my_insert src dest
my_insert() {
	local my_src="$1" my_dest="$2"
	declare -r my_src my_dest

	[ -e "$my_dest" -a "$filetype" == "copy" ] && \
		[ -n "$(find "$my_dest" -type l -printf '%f ')" ] && \
			rm -rf "$my_dest"

	[ ! -e "$my_dest" ] && [[ -z "$MASK" || ! "$my_src" =~ $MASK ]] && (
		if [ "$filetype" == "sym" ]; then
			ln -sf "$my_src" "$my_dest"
		elif [ "$filetype" == "copy" ]; then
			cp -f "$my_src" "$my_dest"
		fi
	)
}

# Insert creates all folders found in $1 and creates symlinks 
# or copies to all files found in $1, creating the dest folder
# if needed. If dest is unspecified, it defaults to the
# basename of src. Filetypes are sym or copy.
# Usage: insert filetype src (dest)
# A phonetic usage would be:
#   "Take this file/folder and insert it into this directory.
#   For every file, symlink/copy it to the destination."
# Note: copying a file to a dest will overwrite the original
#	files if they are symlinks.
# Important Variables:
#	MASK - Use to never insert files matching this regex.
insert() {
	# Note: This function is, along with being recursive, some pretty dense stuff
	#	So if you don't understand what it's doing, don't fuck with it.
	#	Either way, you get to keep the pieces.
	local src="$2" dest="${3:-"."}" filetype="$1" f d
	[ "${src##*/}" != "$src" ] && dest="$dest/${src##*/}"
	declare -r src dest filetype

	# We don't want to use an if statement here, because we want the return code to become
	# the return code of the function.
	[ -e "$src" ] && \
		[ -d "$src" ] && (
			[ -e "$dest" ] && (
				[ ! -d "$dest" ] && return 1
			) || mkdir -p "$dest"

			for f in $(find "$src" -maxdepth 1 -type f -printf '%f '); do
				my_insert "$src/$f" "$dest/$f"
			done

			set $(find "$src" -maxdepth 1 -type d -printf '%f ')
			shift
			for d in $*; do
				insert "$filetype" "$src/$d" "$dest"
			done
		) || my_insert "$src" "$dest"
		
}

# This function loops through all folders provided as arguments and inserts them into .
# The folders can be relative paths located in a folder found in the space delimited
# variable SEARCH_FOLDERS.
# Usage: loop_insert filetype folder1 folder2 ...
loop_insert() {
	: ${SEARCH_FOLDERS:="."}
	local filetype="$1" f d
	declare -r filetype
	shift
	for f in $*; do
		[ "${f##*/}" != "$f" ] && pushd "${f%/*}" > /dev/null
		for d in $SEARCH_FOLDERS; do
			insert "$filetype" "$d/$f"
		done
		popd &> /dev/null
	done
}

loop_insert sym $FILES $FOLDERS
loop_insert copy $CONFIGFOLDERS

[ -z "$(ls bios/)" ] && (
	echo
	echo "*** Put your BIOS file into ~/.pcsx2/bios/"
	echo "    or pcsx2 may not work!"
	echo
)

exec ./pcsx2 "$@"

